﻿using System;
using System.Collections.Generic;
using System.Linq;
using EzDesk.Core.Interface;
using EzDesk.Core.Model;
using EzDesk.Apps.Interface;

namespace EzDesk.Core.ServiceImpl.Services
{
    public partial class CoreClientService : ICoreClientService
    {
        private Dictionary<string, IAppService> _appServicesDic;
        
        # region [ App Functions ]

        public IList<AppFunction> GetAllAppFunctions(Guid userId)
        {
            EzDeskUserAgentType userAgent = _operationProperties.UserAgent;
            
            // 从数据库中直接读取注册的App功能，同时根据权限等进行处理
            IList<AppFunction> appFunctions = _appFunctionRepository.GetAppFunctionsbyUserAgent(userAgent);
            IList<AppFunction> appFuntionsToRemove = new List<AppFunction>();
            foreach (var appFunction in appFunctions)
            {
                // 检查这个用户是否拥有这个权限
                if (appFunction.PermissionNeed != null)
                {
                    string[] permissons = appFunction.PermissionNeed.Split(',');
                    bool hasPermission = (from permissionStr in permissons
                                          where !string.IsNullOrEmpty(permissionStr)
                                          select _coreService.UserHasPermission(permissionStr, userId))
                                          .All(userHasPermission => userHasPermission);

                    // 标记处没有权限的AppFunction
                    if (!hasPermission)
                    {
                        appFuntionsToRemove.Add(appFunction);
                    }
                }
            }

            // 删除无权限的功能
            foreach (var functionToRemove in appFuntionsToRemove)
            {
                appFunctions.Remove(functionToRemove);
            }

            IList<AppFunction> result = appFunctions.Select(p =>
            {
                p.TargetUrl = getTargetUrlFromFuncUrl(p.FuncUrl);
                p.IconUrl = getIconUrl(p.IconName);
                return p;
            })
            .ToList();

            return result;
        }


        public string GetAppFunctionShortcutIds(Guid userId)
        {
            return _appFunctionRepository.GetAppFunctionShortcutIds(userId);
        }

        public void UpdateAppFunctionShortcutSequnce(Guid userId, string shortcutIdSequnces)
        {
            // todo: 验证shorcurId的正确性
            _appFunctionRepository.UpdateAppFunctionShortcut(userId, shortcutIdSequnces);
        }

        #endregion

        public IList<FunctionType> GetAllAppFunctionTypes(Guid userId)
        {
            // todo: 整点缓存来取得功能类型
            var allAppFunctions = GetAllAppFunctions(userId);
            IList<FunctionType> result = new List<FunctionType>();


            foreach (var appFunction in allAppFunctions)
            {
                if (!result.Contains(appFunction.Type, new AppFunctionTypeComparint()))
                {
                    result.Add(appFunction.Type);
                }
            }
            return result;
        }
        
        public IList<AppFunction> GetAppFunctionsByTypeName(Guid userId, string appFunctionTypeName)
        {
            // todo: 整点缓存来取得功能类型
            var allAppFunctions = GetAllAppFunctions(userId);
            return allAppFunctions.Where(p => p.Type.TypeName == appFunctionTypeName).ToList();
        }

    }
}
